package com.zzs.team.Controller;

import com.google.gson.Gson;
import com.zzs.team.Entity.User;
import com.zzs.team.Service.LikeService;
import com.zzs.team.Service.LocationService;
import com.zzs.team.Service.UserService;
import com.zzs.team.Utils.AesUtil;
import com.zzs.team.Utils.HttpRequest;
import com.zzs.team.Utils.UserInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    LocationService locationService;


    /**
     * 用户已经授权情况下登陆处理
     * @param encryptedData
     * @param iv
     * @param code
     * @return
     */
    @ResponseBody
    @PostMapping("/user/getUserInfo")
    public Map getUserInfo(String encryptedData, String iv, String code) {

        System.out.println("请求到达了，encryptedData："+encryptedData+"---iv:"+iv+"---code"+code);

        //1、获取解密后的status（1表示解密成功0表示失败）和用户信息userInfo（User对象）
        Map map = UserInformation.getUserInformation(encryptedData,iv,code);

        if ((Integer)map.get("status")==1){
            //解密成功
            //用户登陆的新信息
            User user = (User) map.get("userInfo");
            //2、检查用户信息
            User u = userService.checkUserByOpenID(user);
            System.out.println("更新后的用户信息："+u.toString());


            Map userInfo= new HashMap();
            userInfo.put("openId",u.getOpenID());
            userInfo.put("nickName",u.getNickName());
            userInfo.put("avatarUrl",u.getAvatar());

            //3、将用户的新信息以Map形式放入map返回
            map.put("userInfo", userInfo);



        }
        System.out.println(map);
        return map;


    }


}
